package com.xbfinal.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbfinal.commonUtils.Exception.GuliException;
import com.xbfinal.eduservice.entity.EduChapter;
import com.xbfinal.eduservice.entity.EduVideo;
import com.xbfinal.eduservice.entity.chapter.ChapterVo;
import com.xbfinal.eduservice.entity.chapter.VideoVo;
import com.xbfinal.eduservice.mapper.EduChapterMapper;
import com.xbfinal.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbfinal.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xbfinal
 * @since 2023-01-09
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 课程大纲列表
     *
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1.根据课程id查询所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        //查询的章节list集合
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);


        //2.根据章节查所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(wrapperVideo);


        List<ChapterVo> finalList = new ArrayList<>();
        //3.遍历章节list集合封装
        //遍历查询的章节list集合
        for (int i = 0; i < eduChapterList.size(); i++) {
            //得到每个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //把eduChapter复制到ChapterVo里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);



            List<VideoVo> videoVoList = new ArrayList<>();
            //4.遍历小节list集合封装
            for (EduVideo eduVideo : eduVideoList) {
                //判断小节的chapter_id和章节里面的id是否一样
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放入小节封装集合
                    videoVoList.add(videoVo);
                }
            }
            //封装之后的小节放入章节里面
            chapterVo.setChildren(videoVoList);
            //ChapterVo放入finalList
            finalList.add(chapterVo);

        }

        return finalList;
    }

    /**
     * 删除章节
     * @param chapterId
     */
    @Override
    public void deleteChapter(String chapterId) {
//        如果章节下面有小节，就不能删除
        QueryWrapper<EduVideo> eduVideoQueryWrapper
                = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(eduVideoQueryWrapper);
        if(count>=1){
            throw new GuliException(20001,"请先删除所有小节");
        }else {
            //没有小节
            int i = baseMapper.deleteById(chapterId);
            if(i<=0){
                throw new GuliException(20001,"删除失败");
            }
        }
    }

    /**
     * 删除章节
     * @param courseId
     */
    @Override
    public void removeChapterById(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
